package com.spring.log;

import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect//注解通知
@Component
@Slf4j
public class LogAspect {

    @Pointcut("execution(* com.spring.service.impl.*.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp)  {

        String className = pjp.getTarget().getClass().getName();
        String method = pjp.getSignature().getName();
        Object result = null;
        try{
            log.info("进入{}的{}方法", className, method);
            long start = System.currentTimeMillis();

            // 执行目标方法
            result = pjp.proceed(pjp.getArgs());

            log.info("{}的{}方法执行完毕，耗时{}ms", className, method, System.currentTimeMillis() - start);
            log.info("返回值：{}", result);
        } catch (Throwable e){
            log.error("{}的{}方法抛出异常：{}", className, method, e.getMessage());
            e.printStackTrace();
        } finally {
            log.info("{}的{}方法结束", className, method);
            return result;
        }

    }
}